package com.android.eb.view;

import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.eb.model.ItemRSS;

public class MensagensRapidasAudio extends Activity {

	ListView listView;
	//Button btnClickMe;
	ArrayList<ItemRSS> listItem = new ArrayList<ItemRSS>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mensagens_rapidas_audio);

		//btnClickMe = (Button) findViewById(R.id.btnClickMe);
		listView = (ListView) findViewById(R.id.lstFastMsg);
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				ItemRSS item = listItem.get(position);

				Intent intent = new Intent(Intent.ACTION_VIEW);

				intent.setData(Uri.parse(item.getUrl()));

				startActivity(intent);
			}
		});

		setRSS();
		/*btnClickMe.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});*/
	}
	
	public void setRSS(){
		try {

			String url = "http://www.estudosdabiblia.net/rss/bd.xml";

			DocumentBuilderFactory dbf = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(url);

			NodeList nodeListItem = doc.getElementsByTagName("item");

			String[] arrayTitles = new String[nodeListItem.getLength()];

			for (int x = 0; x < nodeListItem.getLength(); x++) {
				// título
				String title = nodeListItem.item(x).getChildNodes()
						.item(0).getChildNodes().item(0).getNodeValue();

				// link
				String link = nodeListItem.item(x).getChildNodes()
						.item(1).getChildNodes().item(0).getNodeValue();

				ItemRSS item = new ItemRSS();

				item.setTitle(title);
				item.setUrl(link);

				arrayTitles[x] = item.getTitle();

				listItem.add(item);
			}

			listView.setAdapter(new ArrayAdapter<String>(
					getBaseContext(),
					android.R.layout.simple_list_item_1, arrayTitles));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(getBaseContext(), e.getMessage(),
					Toast.LENGTH_LONG);
		}
	}
}
